package com.zhuzi.common;

import com.zhuzi.common.constant.ConstantCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(
        value = "全局统一返回结果",
        description = "全局统一返回结果"
)
public class R<T> {
    @ApiModelProperty("是否成功 true 成功， false 失败")
    private boolean success;
    @ApiModelProperty("返回码")
    private Integer code;
    @ApiModelProperty("返回消息")
    private String message;
    @ApiModelProperty("返回对象数据")
    private T info;

    public R() {
    }

    public T getInfo() {
        return this.info;
    }

    public void setInfo(T info) {
        this.info = info;
    }

    public R info(T value) {
        this.setInfo(value);
        return this;
    }

    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ConstantCode.OK.getCode());
        r.setMessage(ConstantCode.OK.getName());
        return r;
    }

    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ConstantCode.ERROR.getCode());
        r.setMessage(ConstantCode.ERROR.getName());
        return r;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public static R noAccess() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(401);
        r.setMessage("invalid token");
        return r;
    }

    public static R noVerify() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ConstantCode.NOT_VERIFY.getCode());
        r.setMessage(ConstantCode.NOT_VERIFY.getName());
        return r;
    }

    public static R netError() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ConstantCode.NET_ERROR.getCode());
        r.setMessage(ConstantCode.NET_ERROR.getName());
        return r;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
